package com.howard.modules.user.web;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.howard.modules.base.paging.Paging;
import com.howard.base.util.ResponseUtil;
import com.howard.base.util.StringUtil;
import com.howard.modules.user.User;
import com.howard.modules.user.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class UserAction extends ActionSupport implements Preparable{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserService service;
	
	private User entity;
	
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public User getEntity() {
		return entity;
	}

	public void setEntity(User entity) {
		this.entity = entity;
	}

	@Override
	public void prepare() throws Exception {
		this.clearErrorsAndMessages();
	}
	
	public String toPage(){
//		System.out.println("toPage!!!!!");
//		HttpServletRequest request=ServletActionContext.getRequest();
//		String sessionToken=(String)request.getAttribute("struts.token");	
//		String requestToken=request.getParameter("struts.token");
//		System.out.println("sessionToken="+sessionToken);
//		System.out.println("requestToken="+requestToken);
		return "success";
	}
	
	public void saveOrUpdate(){
		JSONObject result =new JSONObject();
		if(StringUtil.isBlank(entity.getId())){
			service.save(entity);
			result.put("success", "true");
			result.put("msg", "保存成功");
		}else{
			service.update(entity);
			result.put("success", "true");
			result.put("msg", "更新成功");
		}
		try {
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	
	public String gotoList(){
		return SUCCESS;
	}
	
	public void list(){
		String currentPage=ServletActionContext.getRequest().getParameter("page");
		String pageSize=ServletActionContext.getRequest().getParameter("rows");
		
		Paging paging = new Paging();
		if(null==currentPage || "".equals(currentPage)){
			paging.setCurrentPage(1);
		}else{
			paging.setCurrentPage(Integer.parseInt(currentPage));
		}
		if(null==pageSize || "".equals(pageSize)){
			paging.setPageSize(10);
		}else{
			paging.setPageSize(Integer.parseInt(pageSize));
		}
		
		List<User> userList=service.getList(paging);
		JSONArray jsonArray=new JSONArray();
		for(User user:userList){
			JSONObject obj=new JSONObject();
			obj.put("id", user.getId());
			obj.put("entity.name", user.getName());
			obj.put("entity.phone", user.getPhone());
			obj.put("entity.email", user.getEmail());
			obj.put("entity.role", user.getRole());
			jsonArray.add(obj);
		}
		
		JSONObject result=new JSONObject();
		result.put("rows", jsonArray);
		result.put("total",service.count());
		
		try {
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void delete(){
		JSONObject result=new JSONObject();
		User user=this.service.load(entity.getId());
		this.service.delete(user);
		result.put("success", "true");
		result.put("msg", "删除成功");
		try {
			ResponseUtil.write(ServletActionContext.getResponse(), result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
